// AdditionalObj.cpp : Implementation of CAdditionalObj

#include "stdafx.h"
#include "AdditionalObj.h"


// CAdditionalObj
STDMETHODIMP CAdditionalObj::OnConnection(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
{
	spAppCache = Application;//get the outlook application iteself
	ATLASSERT(spAppCache);
	//---------------------------
	//Outlook 10.0.0.6515, 1033 (XP SP3) not totally support Unicode
	//Outlook 11.0.0.8169, 1033 (2003 SP3)
	//Outlook 12.0.0.4518, 1033 (2007)
	//Outlook 12.0.0.6423, 1033 (2007 SP2)
	//Outlook 14.0.0.4760, 1033 (2010)
	//---------------------------
	if(wcscmp(L"Outlook", spAppCache->Name.Detach()) != 0) return S_OK;
	//for outlook 2010
	if(spAppCache->Version.Detach()[1] == '4'){
		Is_Outlook_2010_Version = true;
	}
	else
		Is_Outlook_2010_Version = false;

	return S_OK;
}

STDMETHODIMP CAdditionalObj::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
	return S_OK;
}

STDMETHODIMP CAdditionalObj::OnAddInsUpdate(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CAdditionalObj::OnStartupComplete(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CAdditionalObj::OnBeginShutdown(SAFEARRAY * * custom)
{
	return S_OK;
}

STDMETHODIMP CAdditionalObj::raw_GetCustomUI(BSTR RibbonID, BSTR * RibbonXml)
{
	if(Is_Outlook_2010_Version){
		if(wcscmp(RibbonID , L"Microsoft.Outlook.Explorer") == 0){
			 if(!RibbonXml)
				return E_POINTER;
			*RibbonXml = GetXMLResource(IDR_XML1);
		}
	}
    return S_OK;
}


STDMETHODIMP CAdditionalObj::SyncSettingClicked(IDispatch* ribbon)
{
	if(!spSyncSettingForm)
		spSyncSettingForm = new CSyncSetting();
	spSyncSettingForm->DoModal();
	//if(!spSyncSettingForm)
	//	spSyncSettingForm = new CSyncSetting();
	//spSyncSettingForm->Create(NULL,NULL);
	//spSyncSettingForm->ShowWindow(SW_SHOW);
	return S_OK;
}

HRESULT CAdditionalObj::HrGetResource(int nId, 
    LPCTSTR lpType, 
    LPVOID* ppvResourceData,       
    DWORD* pdwSizeInBytes)
{
    HMODULE hModule = _AtlBaseModule.GetModuleInstance();
    if (!hModule)
        return E_UNEXPECTED;
    HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(nId), lpType);
    if (!hRsrc)
        return HRESULT_FROM_WIN32(GetLastError());
    HGLOBAL hGlobal = LoadResource(hModule, hRsrc);
    if (!hGlobal)
        return HRESULT_FROM_WIN32(GetLastError());
    *pdwSizeInBytes = SizeofResource(hModule, hRsrc);
    *ppvResourceData = LockResource(hGlobal);
    return S_OK;
}

BSTR CAdditionalObj::GetXMLResource(int nId)
{
    LPVOID pResourceData = NULL;
    DWORD dwSizeInBytes = 0;
    HRESULT hr = HrGetResource(nId, TEXT("XML"), 
        &pResourceData, &dwSizeInBytes);
    if (FAILED(hr))
        return NULL;
    // Assumes that the data is not stored in Unicode.
    CComBSTR cbstr(dwSizeInBytes, reinterpret_cast<LPCSTR>(pResourceData));
    return cbstr.Detach();
}

SAFEARRAY* CAdditionalObj::GetOFSResource(int nId)
{
    LPVOID pResourceData = NULL;
    DWORD dwSizeInBytes = 0;
    if (FAILED(HrGetResource(nId, TEXT("OFS"), 
        &pResourceData, &dwSizeInBytes)))
        return NULL;
    SAFEARRAY* psa;
    SAFEARRAYBOUND dim = {dwSizeInBytes, 0};
    psa = SafeArrayCreate(VT_UI1, 1, &dim);
    if (psa == NULL)
        return NULL;
    BYTE* pSafeArrayData;
    SafeArrayAccessData(psa, (void**)&pSafeArrayData);
    memcpy((void*)pSafeArrayData, pResourceData, dwSizeInBytes);
    SafeArrayUnaccessData(psa);
    return psa;
}